<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * 推荐模型
 * Class AppRecommend
 * @package app\common\cachemodel
 */
class AppRecommend extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_recommend';

    protected $cacheModel = 'recommend.recommend';

    /**
     * 缓存获取不同类型的推荐信息
     * @param $type
     * @param $second_type
     * @return array
     * @throws \Exception
     */
    public function getRecommendFromCache($type, $second_type)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type)->setListKeyType($second_type)->ZsGetIocData('list', function () use ($type, $second_type)
        {
            return $this->getRecommendFromDms($type, $second_type);
        });

        return $this->batchGetInfoByIds($ids);
    }

    /**
     * 数据库获取不同类型的推荐id
     * @param $type
     * @param int $second_type
     * @return array
     */
    public function getRecommendFromDms($type, $second_type = 0)
    {
        $where = ['type' => $type, 'second_type' => $second_type, 'status' => 1];
        try
        {
            $data = $this->where($where)->column('id', 'id');
            $data = array_flip_key_value($data);
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    /**
     * 从数据库批量获取推荐信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('status,type,recommend_type,title,des,icon,target,link,extend,create_time', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }
}